package com.autumn.config.services.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.common.api.application.dto.input.PageQueryInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.config.api.services.IConfigurePageService;
import com.autumn.config.entities.IStatusEntity;
import com.autumn.domain.repositories.IDefaultEntityRepository;
import com.autumn.util.data.PageQueryBuilder;

/**
 * 配置服务抽象
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2018-01-01 00:30:16
 * @param <TEntitty>
 *            实体类型
 * @param <TAddInput>
 *            添加输入
 * @param <TModifyInput>
 *            修改输入
 * @param <TOutput>
 *            输出
 * @param <TPageOutput>
 *            页输出
 */
public abstract class AbstractConfigurePageService<TEntity extends IStatusEntity, TQueryEntity extends IStatusEntity, TAddInput, TModifyInput, TOutput, TPageOutput>
		extends AbstractConfigureService<TEntity, TQueryEntity, TAddInput, TModifyInput, TOutput>
		implements IConfigurePageService<TAddInput, TModifyInput, TOutput, TPageOutput> {

	/**
	 * 仓储
	 */
	@Autowired
	protected IDefaultEntityRepository<TEntity> repository;

	private final Class<TPageOutput> pageOutputClass;

	@SuppressWarnings("unchecked")
	public AbstractConfigurePageService() {
		this.pageOutputClass = (Class<TPageOutput>) this.getGenericActualArgumentsTypeMap().get("TPageOutput");
	}

	/**
	 * 获取分页输出类型
	 * 
	 * @return
	 */
	public final Class<TPageOutput> getPageOutputClass() {
		return pageOutputClass;
	}

	@Override
	public IPageQueryResult<TPageOutput> queryByPage(PageQueryInput input) {
		PageQueryBuilder<TQueryEntity> builder = new PageQueryBuilder<>(this.getQueryEntityClass());
		return builder.apply(q -> {
			q.eq("isDelete", false).orderBy("sortId").orderBy("id");
		}).page(input, this.getPageMaxSize()).toPageResult(this.queryRepository, this.getPageOutputClass());
	}

}
